import Vue from 'vue'
import Router from 'vue-router'

import { getToken } from '@/utils/auth'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      
      path: '/',
      component: () => import('@/views/layout'),
      children: [
        {
          name:'home',
          path: '',
          component: () => import('@/views/home')
        },
        {
          path: '/findHouse',
          component: () => import('@/views/findHouse')
        },
        {
          path: '/news',
          component: () => import('@/views/news')
        },
        {
          path: '/profile',
          component: () => import('@/views/profile')
        },
        {
          path: '/search',
          component: () => import('@/views/search')
        },
        {
          name:'cityMap',
          path: '/cityMap',
          component: () => import('@/views/cityMap')
        },
        
      ]
    },
    {
      path: '/detail/:houseCode',
      component: () => import('@/views/detail'),
      props: true
    },
    {
      path: '/login',
      component: () => import('@/views/login')
    },
    {
      path: '/citylist',
      component: () => import('@/views/citylist')
    },
    {
      path: '/user/set',
      component: () => import('@/views/user/set')
    },
    {
      path: '/collect',
      component: () => import('@/views/collect')
    },
    {
      path: '/rent',
      component: () => import('@/views/rent'),
    },
    {
      path: '/rent/add',
      component: () => import('@/views/rent/component/add')
    },
    {
      path:'/add/search',
      component:()=>import('@/views/rent/component/search')
    },
    {
      path: '/404',
      component: () => import('@/views/404')
    },
    {
      path: '*',
      redirect: '/404'
    }
  ]
})

// 前置守卫
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    // 需要登录才可以进入
    if (getToken()) {
      next()
    } else {
      next('/login?redirect=' + to.path)
    }
  } else {
    // 不需要登录就可以进入
    next()
  }
})

export default router
